package com.company.Algorithm;

import java.util.HashMap;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicBoolean;

public class FaceMain {
    public static void main(String[] args) {
        int num = 49;
        int result = sumAllDigits(num);
        // 预期输出4
        System.out.println(result);
    }

    public static int sumAllDigits(int num) {
        // TODO:请在这里输入你的代码解答
        if (num < 10) {
            return num;
        }
        int result = 0;
        while (num > 0) {
            result += num % 10;
            num = num / 10;
        }
        return sumAllDigits(result);
    }

    public static boolean isValid(String s) {
        // TODO:请在这里输入你的代码解答
        HashMap<Character, Character> hashMap = new HashMap<>();
        hashMap.put('(', ')');
        hashMap.put('[', ']');
        hashMap.put('{', '}');
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            Character ch = s.charAt(i);
            if (hashMap.containsKey(ch)) {
                stack.push(ch);
                continue;
            }
            if (stack.size() == 0) {
                return false;
            }
            Character pop = stack.pop();
            if (!ch.equals(hashMap.get(pop))) {
                return false;
            }
        }

        return stack.size() == 0;
    }


    /**
     * 给你一个整数数组 arr，请你帮忙统计数组中每个数的出现次数。
     * 如果每个数的出现次数都是独一无二的，就返回 true；否则返回 false。
     */
    public static boolean uniqueOccurrences(int[] arr) {
        AtomicBoolean result = new AtomicBoolean(true);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            int key = arr[i];
            if (map.containsKey(key)) {
                map.put(key, map.get(key) + 1);
            } else {
                map.put(key, 1);
            }
        }
        HashMap<Integer, Integer> numMap = new HashMap<>();
        map.forEach((k, v) -> {
            if (numMap.containsKey(v)) {
                result.set(false);
                return;
            } else {
                numMap.put(v, 1);
            }
        });
        return result.get();
    }

    /**
     * 给你一个 m * n 的矩阵 grid，矩阵中的元素无论是按行还是按列，都以非递增顺序排列。
     * 请你统计并返回 grid 中 负数 的数目。
     */
    public static int countNegatives(int[][] grid) {
        int num = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] < 0) {
                    num++;
                }
            }
        }
        return num;
    }

    /**
     * 未完成
     *
     * @param arr
     * @return
     */
    public static int maximumSum(int[] arr) {
        int result = 0, start = 0, end = arr.length - 1, finalStart = 0, finalEnd = arr.length - 1;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }

        while (start < end) {
            if (arr[start] <= 0) {
                start++;
            }
            if (arr[end] <= 0) {
                end--;
            }
            finalStart = start;
            finalEnd = end;
            while (start <= end) {
                sum += arr[start];
                start++;
            }
            int newSum = sum;
            while (start < end) {
                newSum = sum - arr[start];
                if (newSum > sum) {
                    sum = newSum;
                    finalStart = start;
                }
            }
        }

        return result;
    }
}
